package com.zsgeek.wisdchain.qutu.view;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zsgeek.wisdchain.qutu.BaseActivity;
import com.zsgeek.wisdchain.qutu.QuTuApplication;
import com.zsgeek.wisdchain.qutu.R;
import com.zsgeek.wisdchain.qutu.base.customControl.NoScrollViewPager;
import com.zsgeek.wisdchain.qutu.base.event.BaseEvent;
import com.zsgeek.wisdchain.qutu.base.event.BaseEventBusHelper;
import com.zsgeek.wisdchain.qutu.base.event.PreferenceHelper;
import com.zsgeek.wisdchain.qutu.base.model.QueryUserModel;
import com.zsgeek.wisdchain.qutu.tools.LogUtil;
import com.zsgeek.wisdchain.qutu.tools.NetUtils;
import com.zsgeek.wisdchain.qutu.tools.TextUtils;
import com.zsgeek.wisdchain.qutu.tools.ToastHelper;
import com.zsgeek.wisdchain.qutu.view.home.EarnEarningFragment;
import com.zsgeek.wisdchain.qutu.view.home.FindFragment;
import com.zsgeek.wisdchain.qutu.view.home.ImportWalletActivity;
import com.zsgeek.wisdchain.qutu.view.home.MeFragment;
import com.zsgeek.wisdchain.qutu.view.home.MessageFragment;
import com.zsgeek.wisdchain.qutu.view.home.PostNotesActivity;
import com.zsgeek.wisdchain.qutu.view.home.adapter.HomePagerAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {
    @BindView(R.id.iv_find)
    ImageView ivFind;
    @BindView(R.id.tv_find)
    TextView tvFind;
    @BindView(R.id.ll_find)
    LinearLayout llFind;

    @BindView(R.id.iv_earn_earning)
    ImageView ivEarnEarning;
    @BindView(R.id.tv_earn_earning)
    TextView tvEarnEarning;
    @BindView(R.id.ll_earn_earning)
    LinearLayout llEarnEarning;

    @BindView(R.id.iv_message)
    ImageView ivMessage;
    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.ll_message)
    LinearLayout llMessage;

    @BindView(R.id.iv_mine)
    ImageView ivMine;
    @BindView(R.id.tv_mine)
    TextView tvMine;
    @BindView(R.id.ll_mine)
    LinearLayout llMine;

    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    @BindView(R.id.view_pager)
    NoScrollViewPager viewPager;

    private boolean isExit = false;
    private int itemPosition = 0;
    public static String address;
    public static QueryUserModel.DataBean userDataBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected boolean isDarkStatusBar() {
        return false;
    }

    @Override
    protected void initView() {
        ivFind.setSelected(true);
        tvFind.setSelected(true);

        viewPager.setOffscreenPageLimit(1);
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new FindFragment());
        fragmentList.add(new EarnEarningFragment());
        fragmentList.add(new MessageFragment());
        fragmentList.add(new MeFragment());
        viewPager.setAdapter(new HomePagerAdapter(getSupportFragmentManager(), fragmentList));
        viewPager.setCurrentItem(itemPosition, false);
    }

    @Override
    protected void initDate(Bundle savedInstanceState) {
        registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
//        address = PreferenceHelper.getInstance().getLastUserName("1Dz1gWJ3Ukvpxah43zZyz9HgRCHqoSs42D");
        address = PreferenceHelper.getInstance().getLastUserName("");
    }

    @OnClick({R.id.ll_find, R.id.ll_earn_earning, R.id.iv_add_release, R.id.ll_message, R.id.ll_mine})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_find:
                isLogin(0);
                break;
            case R.id.ll_earn_earning:
                isLogin(1);
                break;
            case R.id.iv_add_release:
                if (TextUtils.isEmpty(this.address)) {
                    startActivity(new Intent(this, ImportWalletActivity.class));
                    finish();
                } else {
                    startActivity(new Intent(this, PostNotesActivity.class));
                }
                break;
            case R.id.ll_message:
                isLogin(2);
                break;
            case R.id.ll_mine:
                isLogin(3);
                break;
        }
    }

    private void setOneSelected(int type) {
        if (type == 0) {
            ivFind.setSelected(true);
            tvFind.setSelected(true);
            ivEarnEarning.setSelected(false);
            tvEarnEarning.setSelected(false);
            ivMessage.setSelected(false);
            tvMessage.setSelected(false);
            ivMine.setSelected(false);
            tvMine.setSelected(false);
        } else if (type == 1) {
            ivFind.setSelected(false);
            tvFind.setSelected(false);
            ivEarnEarning.setSelected(true);
            tvEarnEarning.setSelected(true);
            ivMessage.setSelected(false);
            tvMessage.setSelected(false);
            ivMine.setSelected(false);
            tvMine.setSelected(false);
        } else if (type == 2) {
            ivFind.setSelected(false);
            tvFind.setSelected(false);
            ivEarnEarning.setSelected(false);
            tvEarnEarning.setSelected(false);
            ivMessage.setSelected(true);
            tvMessage.setSelected(true);
            ivMine.setSelected(false);
            tvMine.setSelected(false);
        } else if (type == 3) {
            ivFind.setSelected(false);
            tvFind.setSelected(false);
            ivEarnEarning.setSelected(false);
            tvEarnEarning.setSelected(false);
            ivMessage.setSelected(false);
            tvMessage.setSelected(false);
            ivMine.setSelected(true);
            tvMine.setSelected(true);
        }
        itemPosition = type;
        viewPager.setCurrentItem(type, true);
    }

    private void isLogin(int type) {
//        if (type != 0 && TextUtils.isEmpty(address)) {
//            startActivity(new Intent(this, ImportWalletActivity.class));
//            finish();
//        } else {
        setOneSelected(type);
//        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x100) {
            switch (itemPosition) {
                case 0:
                    ivFind.setSelected(true);
                    tvFind.setSelected(true);
                    break;
                case 1:
                    ivEarnEarning.setSelected(true);
                    tvEarnEarning.setSelected(true);
                    break;
                case 2:
                    ivMessage.setSelected(true);
                    tvMessage.setSelected(true);
                    break;
                case 3:
                    ivMine.setSelected(true);
                    tvMine.setSelected(true);
                    break;
            }
        }
    }

    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, final Intent intent) {
            if (intent == null)
                return;
            if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                if (!NetUtils.hasNetwork(MainActivity.this)) {
                    QuTuApplication.getInstance().isNetConnect = false;
                    ToastHelper.showToast(R.string.please_check_the_network);
                } else {
                    QuTuApplication.getInstance().isNetConnect = true;
                }
            }
        }
    };

    @Subscribe(threadMode = ThreadMode.MAIN)
    public synchronized void onEventMainThread(BaseEvent event) {
        if (event.getEvent() == BaseEventBusHelper.EVENT_LOGOUT) {
            if (!isExit) {
                isExit = true;
                EventBus.getDefault().unregister(this);
//                startActivity(new Intent(this, LoginActivity.class));
                ToastHelper.showToast(event.getStringValue());
                finish();

            }
        } else if (event.getEvent() == BaseEventBusHelper.EVENT_CHECKNETWORK) {
            ToastHelper.showToast(R.string.please_check_the_network);
        } else if (event.getEvent() == BaseEventBusHelper.EVENT_SwitchIndex) {
            isLogin((int) event.getData());
        } else if (event.getEvent() == BaseEventBusHelper.EVENT_TO_TRADE) {
            isLogin(event.getIntValue());
        }
    }

    // 退出时间
    private long currentBackPressedTime = 0;

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - currentBackPressedTime > 2000) {
                currentBackPressedTime = System.currentTimeMillis();
                ToastHelper.showToast(getString(R.string.exit_tips));
                return true;
            } else {
                finish();
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onDestroy() {
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
        super.onDestroy();
        System.gc();
    }
}
